package controller

import (
	"encoding/json"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/appointmentConsultation/model"
	"strconv"
)

type AppointmentController struct {
	Ctx  iris.Context
	Consultant *model.Consultant
}

func (a *AppointmentController)PostConsultantbookingtime() mvc.Result {

	a.Consultant = new(model.Consultant)

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := a.Consultant.QueryConsultantBookingTime(consultantId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (a *AppointmentController)PostConsultantbookingtimeasync() mvc.Result {

	a.Consultant = new(model.Consultant)

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := a.Consultant.QueryConsultantBookingTimeAsync(consultantId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (a *AppointmentController)PostInsertconsultantbookingtime() mvc.Result {

	a.Consultant = new(model.Consultant)

	dateableListStr := a.Ctx.FormValue("dateableList")
	var dateableList []*model.Dateable
	err := json.Unmarshal([]byte(dateableListStr), &dateableList)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := a.Consultant.InsertConsultantBookingTime(dateableList,consultantId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (a *AppointmentController)PostQueryconsultanttime() mvc.Result {

	a.Consultant = new(model.Consultant)

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	date := a.Ctx.FormValue("date")

	res, err := a.Consultant.QueryConsultantTime(consultantId, date)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (a *AppointmentController)PostCancelreservation() mvc.Result {

	a.Consultant = new(model.Consultant)

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	appointmentRecordIdStr := a.Ctx.FormValue("appointmentRecordId")
	appointmentRecordId, err := strconv.ParseInt(appointmentRecordIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	appointmentDate := a.Ctx.FormValue("appointmentDate")
	period := a.Ctx.FormValue("period")

	res, err := a.Consultant.CancelReservation(appointmentRecordId, consultantId, appointmentDate, period)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (a *AppointmentController)PostInsertrecord() mvc.Result {

	a.Consultant = new(model.Consultant)

	consultantIdStr := a.Ctx.FormValue("consultantId")
	consultantId, err := strconv.ParseInt(consultantIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	consultantName := a.Ctx.FormValue("consultantName")

	orderIdStr := a.Ctx.FormValue("orderId")
	orderId, err := strconv.ParseInt(orderIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	appointmentDate := a.Ctx.FormValue("appointmentDate")
	period := a.Ctx.FormValue("period")

	res, err := a.Consultant.InsertRecord(consultantId, consultantName, orderId, appointmentDate, period)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (a *AppointmentController)PostEvaluate() mvc.Result {

	a.Consultant = new(model.Consultant)

	customerIdStr := a.Ctx.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	customerName := a.Ctx.FormValue("customerName")

	result := a.Ctx.FormValue("result")

	res, err := a.Consultant.Evaluate(result, customerId, customerName)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}




















